from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication
import sys
from gui import Ui_MainWindow
from fuc import *


class mainwindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(mainwindow, self).__init__()
        self.setupUi(self)
        self.name = ''
        self.btn_connect()

    # 定义槽函数
    def btn_connect(self):
        # 绑定槽函数
        self.pushButton.clicked.connect(self.get_data)
        self.pushButton_2.clicked.connect(self.select)
        self.pushButton_3.clicked.connect(self.clear)
        self.pushButton_4.clicked.connect(self.drawing)

    def get_data(self):
        self.label_2.setText('Searching——————————')
        reset()
        self.name = self.lineEdit.text()
        k1 = name_match(self.name)
        url, Bri_intro, flag_end3 = k1[0], k1[1], k1[2]

        if flag_end3:
            for op in end3_lmatch.likely_name:
                self.comboBox.addItem(op)
            QMessageBox.about(self, "成功", "your input is not exact match!\nPlease select the drop-down box")
        elif url is None or Bri_intro is None:
            QMessageBox.critical(self, "错误", "There is no associated output！")
        else:
            information_get(url)
            self.write()
            output(Bri_intro, self.name)  # 输出
            self.label_2.setText(Bri_intro)

    def select(self):
        index = self.comboBox.currentIndex()
        self.name = self.comboBox.itemText(index)
        self.comboBox.clear()
        list_1 = end3_dispose(index)
        url, Bri_intro = list_1[0], list_1[1]
        information_get(url)
        self.write()
        output(Bri_intro, self.name)
        self.label_2.setText(Bri_intro)

    def clear(self):
        self.lineEdit.clear()
        self.label_2.setText('无操作')
        self.tableWidget.setRowCount(0)
        self.tableWidget.clearContents()

    def write(self):
        for i in range(len(outc1.title)):
            lists = [outc1.title[i], outc1.year[i], outc1.download_add[i]]
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)
            for j in range(len(lists)):
                item = QTableWidgetItem(lists[j])
                self.tableWidget.setItem(row, j, item)
        self.tableWidget.resizeColumnToContents(0)

    def drawing(self):
        draw(self.name)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = mainwindow()
    window.show()
    sys.exit(app.exec())
